package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Systemdictionary;
import cn.wolfcode.crm.domain.Systemdictionaryitem;
import cn.wolfcode.crm.mapper.SystemdictionaryMapper;
import cn.wolfcode.crm.mapper.SystemdictionaryitemMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemdictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemdictionaryServiceImpl implements ISystemdictionaryService {
    @Autowired
    private SystemdictionaryMapper systemdictionaryMapper;
    @Autowired
    private SystemdictionaryitemMapper itemMapper;

    public void saveOrUpdate(Systemdictionary entity) {
        if (entity.getId() == null) {
            systemdictionaryMapper.insert(entity);
        } else {
            systemdictionaryMapper.updateByPrimaryKey(entity);
        }
    }

    public void delete(Long id) {
        // 先删除关系
        itemMapper.deleteByParentId(id);
        systemdictionaryMapper.deleteByPrimaryKey(id);
    }

    public void saveOrUpdateItem(Systemdictionaryitem entity) {
        if (entity.getId() == null) {
            itemMapper.insert(entity);
        } else {
            itemMapper.updateByPrimaryKey(entity);
        }
    }

    public void deleteItem(Long itemId) {
        itemMapper.deleteByPrimaryKey(itemId);
    }

    public PageInfo<?> queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());// 只起一次作用
        List<?> list = systemdictionaryMapper.selectForList(qo);
        // System.out.println(1 / 0);
        return new PageInfo<>(list);
    }

    public PageInfo<?> queryItemForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"sequence desc");// 只起一次作用
        List<?> list = itemMapper.selectForList(qo);
        // System.out.println(1 / 0);
        return new PageInfo<>(list);
    }

    public List<Systemdictionaryitem> queryByParentSn(String sn) {
        List<Systemdictionaryitem> systemdictionaryitems = itemMapper.selectByParentSn(sn);
        return systemdictionaryitems;
    }

    public List<Systemdictionaryitem> queryItemForCurriculum() {
        return itemMapper.selectForCurriculum();
    }
}
